package logic

import (
	"context"
	"time"
	"xialou_platform/services/wallet/rpc/internal/svc"
	"xialou_platform/services/wallet/rpc/walletClient"

	"github.com/zeromicro/go-zero/core/logx"
)

type PaymentCallbackLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewPaymentCallbackLogic(ctx context.Context, svcCtx *svc.ServiceContext) *PaymentCallbackLogic {
	return &PaymentCallbackLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// 支付回调
func (l *PaymentCallbackLogic) PaymentCallback(in *walletClient.PaymentCallbackReq) (*walletClient.EmptyBody, error) {
	withdrawal, err := l.svcCtx.WalletWithdrawalModel.FindOne(l.ctx, in.Id)
	if err != nil {
		return nil, err
	}

	// 打款未完成，则继续接受回调
	if withdrawal.Status == 1 || withdrawal.Status == 2 {
		withdrawal.Status = in.Status
		withdrawal.UpdateTime = time.Now()
		withdrawal.Remark = in.Remark
		err = l.svcCtx.WalletWithdrawalModel.Update(l.ctx, withdrawal)
		if err != nil {
			return nil, err
		}
	}

	return &walletClient.EmptyBody{}, nil
}
